package com.ccp.dev.qbdms.tag;

import com.ccp.dev.core.basic.util.PropertyUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.model.SubSystem;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.util.SecurityUtil;
import com.ccp.dev.system.util.SubSystemUtil;
import com.ccp.dev.system.util.SysRoleUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 功能权限标签。
 * <br>
 * 功能：这个标签配合权限分配，可以实现页面上的按钮是否显示，将权限操作控制到按钮。
 * <ul>
 *  <li>标签中 有个别名属性，系统根据该别名控制当前用户是否有该操作的的权限。</li>
 *  <li>标签的使用需要在tld文件中进行配置。</li>
 *  <li>权限标签的写法:<@f.a alias="zdycxsc" css="layui-btn layui-btn-sm" event="delete">标签里的内容</@f.a></li>
 * </ul>
 */
@SuppressWarnings("serial")
public class AnchorTag extends BodyTagSupport {

	/** 链接的样式class */
	private String css;
	/** 链接的 别名*/
	private String alias;
	/** 链接的 name*/
	private String name;
	/** 链接的 id*/
	private String id;
	/** 链接的 event*/
	private String event;
	/**链接的 系统级别*/
	private String systemLevel;
	/**
	 * 当没有权限的时候超链接是否显示。
	 */
	private boolean showNoRight=true;

	private static ThreadLocal<Map<String,Boolean>> threadLocalVar = new ThreadLocal<Map<String,Boolean>>();
	
	public static void cleanFuncRights(){
		threadLocalVar.remove();
	}
	
	public void setShowNoRight(boolean _isShowNoRight){
		this.showNoRight=_isShowNoRight;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}

	public void setEvent(String event) {
		this.event = event;
	}

	public String getSystemLevel() {
		return systemLevel;
	}

	public void setSystemLevel(String systemLevel) {
		this.systemLevel = systemLevel;
	}

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_BODY_BUFFERED;
	}

	private String getOutput() throws Exception {
		SubSystem currentSystem = SubSystemUtil.getCurrentSystem((HttpServletRequest) pageContext.getRequest());
		String systemId = null != currentSystem ? currentSystem.getSystemId() : null;
		SysRole sysRole = SysRoleUtil.getCurrentRole((HttpServletRequest) pageContext.getRequest());
		String roleId = null != sysRole ? sysRole.getRoleId() : null;
		boolean canAccess=false;
		String[] aliasArray=alias.split(",");
		for (int i = 0; i < aliasArray.length; i++) {
			canAccess = SecurityUtil.hasFuncPermission(systemId,roleId,aliasArray[i]);
			if (canAccess){ break; }
		}

		//当没有权限 不显示返回空串。
		if(showNoRight && !canAccess){
			return "";
		}
		String currentSystemLevel = PropertyUtil.getByAlias("xtdj");
		if(StringUtil.isNotEmpty(systemLevel)){
			String[] systemLevelToArr = systemLevel.split(",");
			List<String> systemLevelToList = Arrays.asList(systemLevelToArr);
			if(!systemLevelToList.contains(currentSystemLevel)){
				return "";
			}
		}
		String body = this.getBodyContent().getString();
		StringBuffer content = new StringBuffer("<a ");
		if(id != null) {
			content.append("id=\"" + id + "\" ");
		}
        if(name != null) {
            content.append("name=\"" + name + "\" ");
        }
        if(event != null) {
            content.append("lay-event=\"" + event + "\" ");
        }
        if(systemLevel != null){
			content.append("systemLevel=\"" + systemLevel + "\" ");
		}
		//可以访问的情况。
		if(canAccess) {
			if(css != null) {
				content.append(" class=\"" + css + "\" ");
			}
		} else {
			if(css != null) {
				content.append(" class=\"" + css + " disabled\" ");
			} else {
                content.append(" disabled ");
            }
		}
		content.append(">");
		content.append(body);
		content.append("</a>");
		return content.toString();
	}
	
	@Override
	public int doEndTag() throws JspTagException {
		try {
			String str = getOutput();
			pageContext.getOut().print(str);
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspTagException(e.getMessage());
		}
		return SKIP_BODY;
	}

}
